import Vue from 'vue'
import Router from 'vue-router'
import { getToken } from '@/utils/auth'//token
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'// Progress 进度条样式
import Layout from '@/components/Layout/index'

Vue.use(Router)

const router = new Router({
  mode: 'hash', // require service support history
  // scrollBehavior: () => ({ y: 0 }),
  routes: [
    // 登陆页
    {
      path: '/login',
      name: 'login',
      hidden: true,
      component: () => import('@/views/login/index')
    },
     // 首页
     {
      path: '',
      // name:'index',
      component: Layout,
      meta:{title:'首页',icon:'home'},
      redirect: '/index',
      children: [{
        path: 'index',
        name: 'index',
        component: () => import('@/views/index/index'),
        meta: {title: '首页', icon: 'home'}
      },
      ]
    },
    {
      path: '/pms',
      component: Layout,
      redirect: '/pms/product',
      name: 'pms',
      meta: {title: '商品', icon: 'product'},
      children: [
      {
          path: 'banner',
          name: 'banner',
          component: () => import('@/views/pms/banner/index'),
          meta: {title: '首页轮播', icon: 'product-list'}
      }, 
      {
        path: 'product',
        name: 'product',
        component: () => import('@/views/pms/product/index'),
        meta: {title: '商品列表', icon: 'product-list'}
      },
        {
          path: 'addProduct',
          name: 'addProduct',
          component: () => import('@/views/pms/product/add'),
          meta: {title: '添加商品', icon: 'product-add'}
        },
        {
          path: 'updateProduct',
          name: 'updateProduct',
          component: () => import('@/views/pms/product/update'),
          meta: {title: '修改商品', icon: 'product-add'},
          hidden: true
        },
        {
          path: 'productCate',
          name: 'productCate',
          component: () => import('@/views/pms/productCate/index'),
          meta: {title: '商品分类', icon: 'product-cate'}
        },
        {
          path: 'addProductCate',
          name: 'addProductCate',
          component: () => import('@/views/pms/productCate/add'),
          meta: {title: '添加商品分类'},
          hidden: true
        },
        {
          path: 'updateProductCate',
          name: 'updateProductCate',
          component: () => import('@/views/pms/productCate/update'),
          meta: {title: '修改商品分类'},
          hidden: true
        },
        {
          path: 'productAttr',
          name: 'productAttr',
          component: () => import('@/views/pms/productAttr/index'),
          meta: {title: '商品规格', icon: 'product-attr'}
        },
        {
          path: 'productAttrList',
          name: 'productAttrList',
          component: () => import('@/views/pms/productAttr/productAttrList'),
          meta: {title: '商品属性列表'},
          hidden: true
        },
       
        {
          path: 'brand',
          name: 'brand',
          component: () => import('@/views/pms/brand/index'),
          meta: {title: '品牌管理', icon: 'product-brand'}
        },
        
      ]
    },
    {
      path: '/oms',
      component: Layout,
      redirect: '/oms/order',
      name: 'oms',
      meta: {title: '订单', icon: 'order'},
      children: [
        {
          path: 'order',
          name: 'order',
          component: () => import('@/views/oms/order/index'),
          meta: {title: '订单列表', icon: 'product-list'}
        },
        {
          path: 'orderDetail',
          name: 'orderDetail',
          component: () => import('@/views/oms/order/orderDetail'),
          meta: {title: '订单详情'},
          hidden:true
        },
        {
          path: 'deliverOrderList',
          name: 'deliverOrderList',
          component: () => import('@/views/oms/order/deliverOrderList'),
          meta: {title: '发货列表'},
          hidden:true
        },
        {
          path: 'orderSetting',
          name: 'orderSetting',
          component: () => import('@/views/oms/order/setting'),
          meta: {title: '订单设置', icon: 'order-setting'}
        },
        // {
        //   path: 'returnApply',
        //   name: 'returnApply',
        //   component: () => import('@/views/oms/apply/index'),
        //   meta: {title: '退货申请处理', icon: 'order-return'}
        // },
        // {
        //   path: 'returnReason',
        //   name: 'returnReason',
        //   component: () => import('@/views/oms/apply/reason'),
        //   meta: {title: '退货原因设置', icon: 'order-return-reason'}
        // },
        // {
        //   path: 'returnApplyDetail',
        //   name: 'returnApplyDetail',
        //   component: () => import('@/views/oms/apply/applyDetail'),
        //   meta: {title: '退货原因详情'},
        //   hidden:true
        // }
      ]
    },
  {
    path:'/ums',
    component: Layout,
    redirect: '/ums/admin',
    name: 'ums',
    meta: {title: '权限', icon: 'ums'},
    children: [
      {
        path: 'admin',
        name: 'admin',
        component: () => import('@/views/ums/admin/index'),
        meta: {title: '用户列表', icon: 'ums-admin'}
      },
    ]
  },
    // 404错误页
    {
      path: '/404',
      name:'404',
      hidden:true,
      component: () => import('@/views/404')
    },
    //  {  
    //   path:"*",
    //   component: () => import('@/views/login/index'),
    //   } 
  ]
})

// 注册全局钩子用来拦截导航
const whiteList = ['/login'] // 不重定向白名单
console.log(router);
router.beforeEach((to, from, next) => {
  NProgress.start()
  console.log(getToken());
    if (getToken()) {

      if (to.path === '/login') {
        next({ path: '/' })
      }else{
        if (to.matched.length === 0) {
          next('/404') // 判断此跳转路由的来源路由是否存在，存在的情况跳转到来源路由，否则跳转到404页面
        }
        next() // 如果匹配到正确跳转
      }
      NProgress.done()
      
    } else {
      if (whiteList.indexOf(to.path) !== -1) {
        next()
      } else {
        next('/login')
      }

    }

})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})

// 重置路由
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465



export default router;
